define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'rental_order/index' + location.search,
                    add_url: 'rental_order/add',
                    edit_url: 'rental_order/edit',
                    del_url: 'rental_order/del',
                    multi_url: 'rental_order/multi',
                    import_url: 'rental_order/import',
                    table: 'rental_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'operate', title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name:'rent',
                                    text:'确认出租',
                                    classname:'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon:'fa fa-play',
                                    url:'rental_order/confirmRental',
                                    confirm:'确认出租车辆？',
                                    refresh:true,
                                },
                                {
                                    name:'return',
                                    text:'归还',
                                    classname:'btn btn-xs btn-warning btn-ajax btn-restoreit',
                                    icon:'fa fa-stop',
                                    url:'rental_order/confirmFinish',
                                    confirm:'确认归还车辆？',
                                    refresh:true,
                                }
                            ],
                            events: Table.api.events.operate,
                            formatter: function (value,row,index) {
                                // 打数据补丁，固定写法
                                let that = $.extend({}, this);
                                const table = $(that.table).clone(true);

                                if(row.status==="ordering"){
                                    // 预约状态隐藏归还按钮
                                    $(table).data("operate-return",null);
                                }else if(row.status==="renting"){
                                    // 租赁状态隐藏出租
                                    $(table).data("operate-rent",null);
                                }else {
                                    // 已完成和已取消 同时隐藏归还和出租按钮
                                    $(table).data("operate-return",null);
                                    $(table).data("operate-rent",null);
                                }
                                // 隐藏删除按钮
                                $(table).data("operate-del",null);
                                // 隐藏编辑按钮
                                $(table).data("operate-edit",null);

                                // 打数据补丁，固定写法
                                that = $.extend({}, this);
                                that.table = table;
                                return Table.api.formatter.operate.call(that,value,row,index)
                            }
                        },
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'cars_id', title: __('Cars_id')},
                        {field: 'user_id', title: __('User_id'), operate: 'LIKE'},
                        {field: 'price', title: __('Price'), operate: false},
                        {field: 'startdate', title: __('Startdate'), operate: false, addclass:'datetimerange', autocomplete:false},
                        {field: 'enddate', title: __('Enddate'), operate: false, addclass:'datetimerange', autocomplete:false},
                        {field: 'returncardate', title: __('Returncardate'),operate: false, addclass:'datetimerange', autocomplete:false},
                        {field: 'rental_day', title: __('Rental_day'),operate: false,},
                        {field: 'status', title: __('Status'), searchList: {"ordering":__('Status ordering'),"renting":__('Status renting'),"complete":__('Status complete'),"cancel":__('Status cancel')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate: false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate: false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
